import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Listener;


public class login {

	protected Shell shell;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			login window = new login();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		
		Label lblUserName = new Label(shell, SWT.NONE);
		lblUserName.setBounds(10, 106, 91, 17);
		lblUserName.setText("User Name:");

		final Text UserName = new Text(shell, SWT.BORDER);
		UserName.setBounds(107, 96, 331, 27);

		Label lblPassword = new Label(shell, SWT.NONE);
		lblPassword.setBounds(10, 156, 81, 17);
		lblPassword.setText("Password:");

		final Text Password = new Text(shell, SWT.BORDER);
		Password.setBounds(107, 146, 331, 27);
		
		final Label lblEmpty = new Label(shell, SWT.NONE);
		lblEmpty.setBounds(107, 179, 331, 17);
		lblEmpty.setText("User name or password cannot be empty.");
		lblEmpty.setVisible(false);
		
		final Label lblWrongPassword = new Label(shell, SWT.NONE);
		lblWrongPassword.setBounds(107, 179, 331, 17);
		lblWrongPassword.setText("Invalid user name or password.");
		lblWrongPassword.setVisible(false);
		
		Button btnLogIn = new Button(shell, SWT.NONE);
		btnLogIn.setBounds(84, 205, 91, 29);
		btnLogIn.setText("Log In");
		btnLogIn.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				if (UserName.getText().equals("")||Password.getText().equals("")) {
					lblEmpty.setVisible(true);
				} else {
					// log in to the server
					boolean wrongpassword = false; //for test
					if (wrongpassword) {
						lblWrongPassword.setVisible(true);
					} else {
						shell.close();
						new friendlist().open();
					}
				}
			}
		});
		
		Button btnRegister = new Button(shell, SWT.NONE);
		btnRegister.setBounds(263, 205, 91, 29);
		btnRegister.setText("Register");
		btnRegister.addListener(SWT.Selection, new Listener() {
			public void handleEvent(org.eclipse.swt.widgets.Event event) {
				new register(Display.getCurrent().getActiveShell(), 0).open();
			}
		});

	}
}
